<template>
  <div>
    <van-nav-bar
      title="账号注册"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
        { required: true, message: '请填写用户名' },
        { pattern:/\w{5,}/,message:'请输入五位以上的账号' }
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[
        { required: true, message: '请填写密码' },
        { pattern:/\w{5,}/,message:'请输入五位以上的密码' }
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <router-link to="/login" class="btn">有账号,去登陆</router-link>
  </div>
</template>

<script>
import { getRegister } from '@/api/user'
export default {
  name: 'RegisTer',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (value) {
      const data = await getRegister(value)
      // 判断状态码
      if (data.status === 200) {
        // 200表示成功
        this.$toast.success(data.description)
        this.$router.push('/login')
      } else {
        this.$toast(data.description)
      }
    },
    // 返回
    onClickLeft () {
      this.$router.push('/login')
    }
  }

}
</script>

<style lang="less" scoped>
.btn{
    display: block;
    text-align: center;
    color: black;
  }
</style>
